﻿@namespace BlazorDemo.Pages.CaseViews

<div class="demo-description">
    <h2><DemoNavLink Link="Calendar#DayCellCustomization" />Day Cell Customization</h2>
    <p>Use the calendar’s <a class="helplink" target="_blank" href="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxCalendar-1.DayCellTemplate">DayCellTemplate</a> property to customize day cells.</p>
</div>

<div class="card demo-card border-0 bg-transparent">
    <div class="card-body p-0">
        <DxCalendar T="DateTime">
            <DayCellTemplate>
                <a class="@GetCssClassNames(context)">@context.Day.ToString()</a>
            </DayCellTemplate>
        </DxCalendar>
    </div>
</div>

<CodeSnippet_Editor_Calendar_DayCellCustomization></CodeSnippet_Editor_Calendar_DayCellCustomization>

@code {
    CalendarData data = new CalendarData();

    string GetCssClassNames(DateTime date) {
        string result = string.Empty;

        if(data.PersonalDays.Exists(d => DaysEqual(d, date)))
            result = "font-weight-bold text-success";
        if(data.Holidays.Exists(d => DaysEqual(d, date)))
            result = "text-danger";
        if(data.BirthDates.Exists(d => DaysEqual(d, date)))
            result += "font-weight-bold text-info";

        return result;
    }
    bool DaysEqual(DateTime date1, DateTime date2) {
        return (date1.Year == date2.Year && date1.DayOfYear == date2.DayOfYear);
    }

    class CalendarData {

        public CalendarData() {
            DateTime baseDate = DateTime.Today;
            PersonalDays = GetPersonalDays(baseDate);
            Holidays = GetHolidays(baseDate);
            BirthDates = GetBirthDates(baseDate);
        }

        public List<DateTime> PersonalDays { get; }
        public List<DateTime> Holidays { get; }
        public List<DateTime> BirthDates { get; }

        List<DateTime> GetPersonalDays(DateTime baseDate) {
            return new List<DateTime>() {
                baseDate.AddDays(-7),
                baseDate.AddDays(-2),
                baseDate.AddDays(-1),
                baseDate.AddDays(3),
                baseDate.AddDays(4),
                baseDate.AddDays(9),
                baseDate.AddDays(10),
                baseDate.AddDays(12),
                baseDate.AddDays(15)
            };
        }

        List<DateTime> GetHolidays(DateTime baseDate) {
            return new List<DateTime>() {
                baseDate.AddDays(-14),
                baseDate.AddDays(-8),
                baseDate.AddDays(12),
                baseDate.AddDays(13)
            };
        }

        List<DateTime> GetBirthDates(DateTime baseDate) {
            return new List<DateTime>() {
                baseDate.AddDays(-20),
                baseDate.AddDays(-5),
                baseDate,
                baseDate.AddDays(1),
                baseDate.AddDays(6),
                baseDate.AddDays(7),
                baseDate.AddDays(17)
            };
        }
    }
}
